<script lang="ts" setup>
import { computed, h } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { formatDate } from '@vben/utils';

import { DescriptionChange } from '#/components/description-change';
import DictTag from '#/components/dict-tag/dict-tag.vue';

interface DescriptionItemSchema {
  label: string;
  field: string;
  span?: number;
  render?: (val: any) => any;
}

const props = defineProps<{ data?: any }>();
const formData = computed(() => props.data || {});

function formatDateSafe(
  date: null | string | undefined,
  format = 'YYYY-MM-DD',
) {
  if (!date) return '';
  const d = new Date(date);
  return Number.isNaN(d.getTime()) ? '' : formatDate(d, format);
}

/** 岗位信息 Schema */
const postSchema = computed<DescriptionItemSchema[]>(() => [
  { label: '任职单位', field: 'postUnit', span: 24 },
  {
    label: '开始时间',
    field: 'startDate',
    render: (val) => formatDateSafe(val),
    span: 12,
  },
  {
    label: '结束时间',
    field: 'endDate',
    render: (val) => formatDateSafe(val),
    span: 12,
  },
  {
    label: '岗位类别',
    field: 'postType',
    render: (val) => h(DictTag, { type: DICT_TYPE.PER_POST_TYPE, value: val }),
    span: 12,
  },
  {
    label: '岗位等级',
    field: 'postLevel',
    render: (val) => h(DictTag, { type: DICT_TYPE.PER_POST_LEVEL, value: val }),
    span: 12,
  },
  { label: '任职文件', field: 'postFile', span: 24 },
  { label: '备注', field: 'remarks', span: 24 },
  {
    label: '状态',
    field: 'status',
    render: (val) => h(DictTag, { type: DICT_TYPE.COMMON_STATUS, value: val }),
    span: 12,
  },

  {
    label: '更新时间',
    field: 'updateTime',
    render: formatDateSafe,
    span: 12,
  },
]);
</script>

<template>
  <DescriptionChange
    v-if="formData"
    :info="formData"
    :schema="postSchema"
    :change-list="formData.changeContent?.fieldChanges || []"
    :highlight-mode="formData.highlightMode"
    :change-type="formData.changeType"
    :label-width="120"
  />
</template>
